package com.iogogogo.activity.util;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.iogogogo.activity.base.IJacksonConfiguration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.json.JsonParserFactory;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * Created by tao.zeng on 2022/6/16.
 */
@Slf4j
public class JacksonParse implements IJacksonConfiguration {

    /**
     * To json string.
     *
     * @param bean the bean
     * @return the string
     * @throws JsonProcessingException the json processing exception
     */
    public static String toJson(Object bean) throws JsonProcessingException {
        return OBJECT_MAPPER.writeValueAsString(bean);
    }

    /**
     * Try to json string.
     *
     * @param bean the bean
     * @return the string
     */
    public static String tryToJson(Object bean) {
        try {
            return OBJECT_MAPPER.writeValueAsString(bean);
        } catch (JsonProcessingException e) {
            log.warn("JsonProcessingException,", e);
        }
        return bean instanceof Collection ? EMPTY_LIST : EMPTY_OBJECT;
    }

    /**
     * To json pretty printer string.
     *
     * @param bean the bean
     * @return the string
     */
    public static String toJsonPrettyPrinter(Object bean) {
        try {
            return OBJECT_MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(bean);
        } catch (JsonProcessingException e) {
            log.warn("JsonProcessingException,", e);
        }
        return bean instanceof Collection ? EMPTY_LIST : EMPTY_OBJECT;
    }

    /**
     * Parse t.
     *
     * @param <T>  the type parameter
     * @param json the json
     * @param clz  the clz
     * @return the t
     * @throws IOException the io exception
     */
    public static <T> T parse(String json, Class<T> clz) throws IOException {
        if (!StringUtils.hasLength(json)) return null;
        return OBJECT_MAPPER.readValue(json, clz);
    }

    /**
     * Parse t.
     *
     * @param <T>  the type parameter
     * @param json the json
     * @param type the type
     * @return the t
     * @throws IOException the io exception
     */
    public static <T> T parse(String json, TypeReference<T> type) throws IOException {
        if (!StringUtils.hasLength(json)) return null;
        return OBJECT_MAPPER.readValue(json, type);
    }

    /**
     * Try parse t.
     *
     * @param <T>  the type parameter
     * @param json the json
     * @param clz  the clz
     * @return the t
     */
    public static <T> T tryParse(String json, Class<T> clz) {
        if (!StringUtils.hasLength(json)) return null;
        try {
            return OBJECT_MAPPER.readValue(json, clz);
        } catch (IOException e) {
            log.warn("IOException,", e);
        }
        return null;
    }

    /**
     * Try parse t.
     *
     * @param <T>  the type parameter
     * @param json the json
     * @param type the type
     * @return the t
     */
    public static <T> T tryParse(String json, TypeReference<T> type) {
        if (!StringUtils.hasLength(json)) return null;
        try {
            return OBJECT_MAPPER.readValue(json, type);
        } catch (IOException e) {
            log.warn("IOException,", e);
        }
        return null;
    }

    /**
     * Parse map map.
     *
     * @param json the json
     * @return the map
     */
    public static Map<String, Object> parseMap(String json) {
        return JsonParserFactory.getJsonParser().parseMap(json);
    }

    /**
     * Parse list list.
     *
     * @param json the json
     * @return the list
     */
    public static List<Object> parseList(String json) {
        return JsonParserFactory.getJsonParser().parseList(json);
    }
}
